using Admin.NET.Core;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MOKA.TPM.Application.Service.Dto;
using MOKA.TPM.Entity;
using MOKA.TPM.Utility.Const;
using SqlSugar;
using System.ComponentModel;

namespace MOKA.TPM.Service;

/// <summary>
/// 设备点检报表服务
/// </summary>
[ApiDescriptionSettings(TPMConst.GroupName, Order = 870)]
public class Service_CheckupReports : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Checkup> _rep;
    private readonly UserManager _userManager;
    private IHttpContextAccessor _accessor;//获取请求上下文_userRepository
    private readonly ISqlSugarClient _db;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// sadf
    /// <param name="db"></param>
    /// <param name="rep"></param>
    /// <param name="userManager"></param>
    /// <param name="accessor"></param>
    public Service_CheckupReports(ISqlSugarClient db, SqlSugarRepository<Checkup> rep, UserManager userManager, IHttpContextAccessor accessor)
    {
        _db = db;
        _rep = rep;
        _userManager = userManager;
        _accessor = accessor;
    }

    /// <summary>
    /// 获取各周期类型，各设备管理员需点检的设备数量🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "GetQtyCheckup")]
    [DisplayName("获取各周期类型，各设备管理员需点检的设备数量🔖")]
    [AllowAnonymous]
    public async Task<List<ToCheckupQty>> GetQtyCheckup([FromQuery] Guid? CheckupCycle, [FromQuery] string? ManagerNo, [FromQuery] string? CheckupCycleName = null)
    {
        var result = await _rep.AsTenant().QueryableWithAttr<Equipment>()
                        .LeftJoin<Employee>((e, e2) => e.Manager == e2.LoginName)
                        .InnerJoin<CheckupItemTPMCategory>((e, e2, c) => e.TPMCategoryId == c.TPMCategoryId)
                        .LeftJoin<SysDictDetail>((e, e2, c, sdd) => c.CheckupCycle == sdd.Id)
                        .WhereIF(CheckupCycle.HasValue, (e, e2, c, sdd) => c.CheckupCycle == CheckupCycle)
                        .WhereIF(!string.IsNullOrWhiteSpace(CheckupCycleName), (e, e2, c, sdd) => (sdd.ItemName == CheckupCycleName))
                        .WhereIF(!string.IsNullOrWhiteSpace(ManagerNo), (e, e2, c, sdd) => (e.Manager == ManagerNo))
                        .WhereIF(string.IsNullOrWhiteSpace(ManagerNo), (e, e2, c, sdd) => (e.Manager != "" && e.Manager != null && e2.IsDimission == false))
                        .GroupBy((e, e2, c, sdd) => new { c.CheckupCycle, sdd.ItemName, e.Manager, e2.EmployeeName, e2.DepartmentName })
                        .OrderBy((e, e2, c, sdd) => new { c.CheckupCycle, e2.DepartmentName, e.Manager })
                        .Select((e, e2, c, sdd) => new ToCheckupQty()
                        {
                            CheckupCycle = c.CheckupCycle,
                            CheckupCycleName = sdd.ItemName,
                            ManagerNo = e.Manager,
                            EmployeeName = e2.EmployeeName,
                            DepartmentName = e2.DepartmentName,
                            Count = SqlFunc.AggregateDistinctCount(e.EquipmentNo)
                        })

                        .ToListAsync();

        return result;
    }

    /// <summary>
    /// 获取各点检周期类型，当前周期，各个检点员已经点检的设备数量🔖
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "GetQtyCheckuped")]
    [DisplayName("获取各点检周期类型，当前周期，各个检点员已经点检的设备数量🔖")]
    [AllowAnonymous]
    public async Task<List<CheckupedQty>> GetQtyCheckuped([FromQuery] Guid? CheckupCycle, [FromQuery] string? CurrentCycle, [FromQuery] string? CheckupMan, [FromQuery] string? CheckupCycleName = null)
    {
        var lists = await _rep.Context
            .AsTenant().QueryableWithAttr<Checkup>()
                    .LeftJoin<Employee>((e, e2) => e.CheckupMan == e2.LoginName)
                    .LeftJoin<SysDictDetail>((e, e2, sdd) => e.CheckupCycle == sdd.Id)
                    // .Where((e, e2, sdd) => sdd.ItemName == "每日" && e.CurrentCycle == "20241122"
                    // && e.CheckupMan == "huangwei.zhou")
                    .WhereIF(CheckupCycle.HasValue, (e, e2, sdd) => e.CheckupCycle == CheckupCycle)
                    .WhereIF(!string.IsNullOrEmpty(CheckupCycleName?.Trim()), (e, e2, sdd) => sdd.ItemName == CheckupCycleName)
                    .WhereIF(!string.IsNullOrEmpty(CurrentCycle?.Trim()), (e, e2, sdd) => e.CurrentCycle == CurrentCycle)
                    .WhereIF(!string.IsNullOrEmpty(CheckupMan?.Trim()), (e, e2, sdd) => e.CheckupMan == CheckupMan)
                    .GroupBy((e, e2, sdd) => new { e.CheckupCycle, sdd.ItemName, e.CurrentCycle, e.CheckupMan, e2.EmployeeName, e2.DepartmentName })
                    .OrderBy(e => new { e.CheckupCycle, e.CurrentCycle, e.CheckupMan })
                    .Select((e, e2, sdd) => new CheckupedQty()
                    {
                        CheckupCycle = e.CheckupCycle,
                        CheckupCycleName = sdd.ItemName,
                        CurrentCycle = e.CurrentCycle,
                        CheckupMan = e.CheckupMan,
                        EmployeeName = e2.EmployeeName,
                        DepartmentName = e2.DepartmentName,
                        Count = SqlFunc.AggregateCount(e.CheckupMan)
                    })
                    .ToListAsync();

        return lists;
    }
}